﻿package com.sms.monitor.ui.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.sms.monitor.R;
import com.sms.monitor.constant.Constant;
import com.sms.monitor.utils.SharedPreferencesUtil;
import com.sms.monitor.utils.Util;
/**
 * 拦截号码设置界面。
 * 
 *
 */
public class PhoneSettingActivity extends Activity implements OnClickListener,OnItemLongClickListener {
	private ListView				mPhones;
	private Button					mAddPhone;
	private EditText				mPhone;
	private List<String>			mPhoneList	= new ArrayList<String>();
	private ArrayAdapter<String>	mPhoneAdapter;
	private String					mPhoneStr;
	private Set<String>				mPhoneSet;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configration_phone_layout);
		findViews();
	}

	private void findViews() {
		// 
		mPhones = (ListView) findViewById(R.id.lv_phones);
		mAddPhone = (Button) findViewById(R.id.btn_add_phone);
		mPhone = (EditText) findViewById(R.id.et_phone);
		mPhoneStr = SharedPreferencesUtil.getValue(this, Constant.KEY_SMS_SPF_PHONE);
		
		mPhoneSet = Util.converStringToSet(mPhoneStr);
		
		//设置界面显示。
		SharedPreferencesUtil.putValue(this, Constant.KEY_SMS_SPF_PHONE, Util.converSetToString(mPhoneSet));
		mPhoneStr = SharedPreferencesUtil.getValue(this, Constant.KEY_SMS_SPF_PHONE);
		mPhoneSet = Util.converStringToSet(mPhoneStr);
		mPhoneList.addAll(mPhoneSet);
		mPhoneAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPhoneList);
		mPhones.setAdapter(mPhoneAdapter);
		mAddPhone.setOnClickListener(this);
		mPhones.setOnItemLongClickListener(this);
	}

	/**
	 * 添加拦截号码
	 */
	@Override
	public void onClick(View v) {
		// 
		switch (v.getId()) {
		case R.id.btn_add_phone:
			String phone = mPhone.getText().toString().trim();
			if (!TextUtils.isEmpty(phone)) {
				mPhoneSet.add(phone);
				SharedPreferencesUtil.putValue(this, Constant.KEY_SMS_SPF_PHONE, Util.converSetToString(mPhoneSet));
				mPhoneList.clear();
				mPhoneList.addAll(mPhoneSet);
				mPhoneAdapter.notifyDataSetChanged();
				mPhone.setText("");

			}

			break;

		default:
			break;
		}
	}

	/**
	 * 长按删除号码
	 */
	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int position, long arg3) {
		// 
		AlertDialog.Builder  builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.notice)).setMessage(getString(R.string.delete_or_not)).setNegativeButton(getString(R.string.delete), new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 
				mPhoneSet.remove(mPhoneList.get(position));
				SharedPreferencesUtil.putValue(PhoneSettingActivity.this, Constant.KEY_SMS_SPF_PHONE, Util.converSetToString(mPhoneSet));
				mPhoneList.clear();
				mPhoneList.addAll(mPhoneSet);
				mPhoneAdapter.notifyDataSetChanged();
				mPhone.setText("");
				dialog.cancel();
				
			}
		}).setPositiveButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 
				dialog.cancel();
			}
		}).show();
		return false;
	}
}
